// JavaScript Document

//function cho phan chay sidebar
$(function(){
	if($(".side-bar-wrapper").length > 0 && $(".side-bar-wrapper").height() <= $('.main-content').height() + 120){
		$window = $(window),
		$sidebar = $(".side-bar-wrapper"),
		sidebarTop = $sidebar.position().top,
		sidebarHeight = $sidebar.height(),
		$footer = $(".footer-wapper"),
		miny = $('.header').height() - 10,
		footerTop = $footer.position().top ,    
		setSidebar();
		maxy = $('.wrapper').height() - $('.footer-wapper').height() ;
		//alert('Here');
		$window.scroll(function(event) {
	
			scrollTop = $window.scrollTop(),
			miny = $(".header").height() + 46;
			if($window.scrollTop() > miny && $window.scrollTop() + $sidebar.height() < $('.header').height()+$('.main-content').height() + $('.comment-template').height()){
				$sidebar.addClass('fix');
				$sidebar.css('top', 0);
				$sidebar.css('margin-top','');
	
			}
			else if($window.scrollTop() + $sidebar.height() > $('.header').height()+$('.main-content').height() + $('.comment-template').height()){
				$sidebar.removeClass('fix');
				$sidebar.css('top', '');
				$sidebar.css('margin-top',$('.header').height()+$('.main-content').height() + $('.comment-template').height() - $sidebar.height() - 200);
			}
			else if($window.scrollTop() < miny){
				$sidebar.removeClass('fix');
			}
		});
	}
	$(window).resize(function(){
		setSidebar();
	});
});

function setSidebar(){
	margin = ($(window).width() - 900)/2;
	$sidebar.css({"left":margin});
}
//function cho phần comment
$(function(){
	$("#author").focus(function() {
		if( $(this).val() == "Vui lòng nhập tên (*)" )
			$(this).val("");
	});
	$("#email").focus(function() {
		if( $(this).val() == "Địa chỉ mail (*)" )
			$(this).val("");
	});
	$("#url").focus(function() {
		if( $(this).val() == "Website" )
			$(this).val("");
	});
	$("#title").focus(function() {
		if( $(this).val() == "Chủ đề" )
			$(this).val("");
	});
	$("input").blur(function(){
		if( $(this).val() == "" ) {
			if( $(this).is("input[name='author']"))
				$(this).val("Vui lòng nhập tên (*)");
			if( $(this).is("input[name='email']"))
				$(this).val("Địa chỉ mail (*)");
			if( $(this).is("input[name='url']"))
				$(this).val("Website");
				if( $(this).is("input[name='title']"))
				$(this).val("Chủ đề");
		}
	});
});


//jQuery cho cai filter
$(document).ready(function() {	
	var menu = $('.menu-gallery'),
		menu_item = menu.find('li'),
		gallery = $(".gallery-item");
	gallery.find('li').each(function(){
		$(this).attr("rel", $(this).attr("rel").replace(" ","")); 
	});
	if(!gallery.find('li').is(":animated")){
			menu.find('a').click(function() {	
			thisItem 	= $(this).attr('rel').replace(" ","");
			if(thisItem != "All") {
				gallery.find('li[rel='+thisItem+']')
					.show(500);
																	
				gallery.find('li[rel!='+thisItem+']')
					.hide(500);
			} else {
				gallery.find('li')
					.show(500);
			}
		})
	}
	gallery.find('li img').animate({'opacity' : 0.5}).hover(function() {
		$(this).animate({'opacity' : 1});
	}, function() {
		$(this).animate({'opacity' : 0.5});
	});
});


//********* đăng ký jQuery fancybox, xem hình trang single *********
/*
$(document).ready(function() {
	$('div#the-content a').attr('rel', 'group');
	if($("a[rel=group]").length > 0){
		$("a[rel=group]").fancybox({
			'centerOnScroll'		:	false,
		});
	}
});
*/
//********* end đăng ký jQuery fancybox, xem hình trang single *********


//********* đăng ký jQuery fix kich co hinh trong single *********
$(function() {
	maxwidth = $(".content").width();
	
	$images = $(".content img");
	$images.each(function(){
		var imgwidth = $(this).width();
		
		if( imgwidth < maxwidth ) //Neu width cua hinh nho hon thi canh giua
		{
			var marginleft = (maxwidth - imgwidth) / 2;
			$(this).css('margin-left',marginleft);
		}
		else //Neu width cua hinh lon hon thi fix cho bang voi maxwidth
		{
			$(this).attr('width',maxwidth);
			$(this).attr('height','');
		}
		
		$('.content').find('img').load(function(){
			 if (this.complete){
				$(this).parent().each(function(){
					if($(this).is("a")){
						content = $(this).contents();
						$(this).replaceWith(content);
					}
				});
			 }
		});
		
	});
	
	$('#the-content').find('img').each(function(){
		alt = $(this).attr("alt");
		$(this).attr("alt", alt.replace("fancydesign.vn-", ''));
	});
	$('.wrapper-gallery .menu-gallery ul li').click(function(){
		$('.wrapper-gallery .menu-gallery ul li').removeClass("gallery-current");
		$(this).addClass("gallery-current");
	});
	
	//alert($('#the-content').find('img').lenght());
	
	
});
//*********end đăng ký jQuery fix kich co hinh trong single *********



//********* Back to top *********
$(document).ready(function(){
	
	//$('.footer').append('<p id="back-to-top">Top</p>');
	
	$('#back-to-top').click(function(){
		$('html, body').animate({scrollTop:0}, 500);
	});
});
//********* end Back to top *********


function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-+\s]+")|([\w-+]+(?:\.[\w-+]+)*)|("[\w-+\s]+")([\w-+]+(?:\.[\w-+]+)*))(@((?:[\w-+]+\.)*\w[\w-+]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][\d]\.|1[\d]{2}\.|[\d]{1,2}\.))((25[0-5]|2[0-4][\d]|1[\d]{2}|[\d]{1,2})\.){2}(25[0-5]|2[0-4][\d]|1[\d]{2}|[\d]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
};

















